package com.zhinan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhinan.dao.MenuMapper;
import com.zhinan.entity.Menu;
import com.zhinan.service.MenuService;
import com.zhinan.vo.MenuVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TuTu_WorkManager
 * @Author: ZhiNan
 * @Date: 2023-04-07 15:41
 * @Version: 1.0
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuVo> implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    /**
     * 查询菜单信息
     */
    public List<MenuVo> selectIndexMenu(Menu menu) {
        LambdaQueryWrapper<MenuVo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MenuVo::getParentId, 0);
        List<MenuVo> fatherMenus = menuMapper.selectList(queryWrapper);
        List<MenuVo> menuVos = fatherMenus.stream().map((item) -> {
            LambdaQueryWrapper<MenuVo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(MenuVo::getParentId, item.getId());
            List<MenuVo> sonMenus = menuMapper.selectList(wrapper);
            item.setChildren(sonMenus);
            return item;
        }).collect(Collectors.toList());
        return menuVos;
    }
}
